package com.fitzone.web.handler;

import com.fitzone.common.exception.BaseException;
import com.fitzone.common.result.Result;
import com.fitzone.common.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获没有权限异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        e.printStackTrace();
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }

    /**
     * 捕获业务异常
     */
    @ExceptionHandler(BaseException.class)
    public Result exceptionHandler(BaseException e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result handleRuntimeException(RuntimeException e) {
        e.printStackTrace();
        return Result.fail().message(e.getMessage());
    }

}